﻿using luck_roll.Config;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace luck_roll
{
    public partial class MusicSelectFrm : Form
    {
        public MusicSelectFrm()
        {
            InitializeComponent();
        }

        private void MusicSelectFrm_Load(object sender, EventArgs e)
        {

            string currentPath = Environment.CurrentDirectory;
            string folderPath = Path.Combine(currentPath, "resources");

            this.chbMusicStatus.Checked = ConfigHelper.currentAppConfig.BackgroundMusicEnable;
            this.tbFile.Text = Path.Combine(folderPath, ConfigHelper.currentAppConfig.BackgroundMusic);
            this.chbLuckNameStatus.Checked = ConfigHelper.currentAppConfig.LuckNamePlayerEnable;
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Multiselect = false;

                ofd.Filter = "音乐文件 (*.mp3; *.wav;*.m4a)|*.mp3;*.wav;*.m4a";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    if (!File.Exists(ofd.FileName))
                    {
                        MessageBox.Show("文件不存在");
                        return;
                    }
                }
                else
                {
                    return;
                }

                string fileName = Path.GetFileName(ofd.FileName);
                string currentPath = Environment.CurrentDirectory;
                string folderPath = Path.Combine(currentPath, "resources");
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                String newFilePath = Path.Combine(folderPath, fileName);

                File.Copy(ofd.FileName, newFilePath, true);



                this.tbFile.Text = newFilePath;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            ConfigHelper.currentAppConfig.BackgroundMusic = Path.GetFileName(this.tbFile.Text);
            ConfigHelper.currentAppConfig.BackgroundMusicEnable = this.chbMusicStatus.Checked;
            ConfigHelper.currentAppConfig.LuckNamePlayerEnable = this.chbLuckNameStatus.Checked;
            ConfigHelper.SaveConfig();
            this.Close();
        }
    }
}
